import WeexNativeModule from "../../common/WeexNativeModule";
import StringUtils from '../../common/util/StringUtils';
import DateUtil from  '../../common/util/DateUtil';
let instance;
let pageImpl;
let taskcode;
let isfirstcreate;
let dom;
class EMNewMainEditControl{
    constructor(vuepage){
        pageImpl =this;
        instance = vuepage;
        pageImpl.initEditData();
        isfirstcreate = true;
        dom = weex.requireModule('dom');
    }
    initEditData(){
        WeexNativeModule.getParam(function (data) {
            let result = JSON.parse(data.param);
            // WeexNativeModule.alert(result)
            taskcode = result.taskcode;
            instance.saveData.id = result.taskcode;
            instance.saveData.carOutId = result.caroutid;
            instance.saveData.userName = result.patientname;
            instance.saveData.gender = result.patientgender;
            instance.saveData.age = result.patientage;
            instance.saveData.mobile = result.mobile;
            instance.saveData.address = result.address;
            instance.saveData.chiefComplaint = result.chiefComplaint;
            instance.saveData.history = result.history;
            instance.iscomebyself = result.iscomebyself;
        })
    }
    getIdcard(){
        WeexNativeModule.getIdCardResult(function (res) {
            instance.saveData.userName = res.name;
        })
    }
    finish(){
        WeexNativeModule.finish();
    }

    onInputFBAddress(event) {
        if(isfirstcreate){
            isfirstcreate = false
            return
        }
        if (instance.issetaddressing) {
            instance.issetaddressing = false;
            return
        }
        let value = event.value;
        let el = instance.$refs.addressitem;
        dom.scrollToElement(el, {})
        WeexNativeModule.searchAddress(value, function (res) {
            instance.listData = [];
            let result = res.result;
            for (let item in result) {
                let value = result[item]
                instance.listData.push(value)
            }
            if (instance.listData.length > 0) {
                instance.ishjaddressshow = true;
            } else {
                instance.ishjaddressshow = false;
            }
        })
    }

    setFBAddress(item) {
        instance.saveData.address = item.district + item.name;
        instance.ishjaddressshow = false;
        instance.issetaddressing = true;
    }

    save(){
        if(instance.saveData.age  && instance.saveData.age.length > 3){
            WeexNativeModule.toast("请输入正确年龄")
            return
        }
        WeexNativeModule.saveData(instance.saveData);
    }
}

export default EMNewMainEditControl;